﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MicroLibraryAssembly;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    clsUser objThis = new clsUser();
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void imgbtnLogin_Click(object sender, ImageClickEventArgs e)
    {
        SqlDataReader loginDataReader;
        try
        {
            if (Page.IsValid)
                if (txtUserName.Text.Trim() != null && txtPassword.Text.Trim() != null)
                {
                    loginDataReader = objThis.CheckLogin(txtUserName.Text.Trim(), txtPassword.Text.Trim(), "", "");
                    if (loginDataReader.HasRows)
                    {

                        loginDataReader.Read();
                        int UserType = Convert.ToInt32((loginDataReader["UserType"].ToString()));
                        if (!Convert.ToBoolean(loginDataReader["IsActive"]))
                        {
                            CommonClass.PrintError(litLoginMsg, "LOGIN_INACTIVE");
                            return;
                        }
                        if (UserType == 2)
                        {
                            HttpCookie CookieUserID = new HttpCookie("CookieUserID");
                            CookieUserID["ID"] = (loginDataReader["ID"].ToString());
                            CookieUserID["Username"] = txtUserName.Text.Trim();
                            CookieUserID["UserType"] = UserType.ToString();
                            CookieUserID.Expires = DateTime.Now.AddDays(30);
                            Response.Cookies.Add(CookieUserID);


                            Response.Redirect("index.aspx", false);
                        }
                        else
                        {
                            loginDataReader.Close();
                            CommonClass.PrintError(litLoginMsg, "LOGIN_ERROR");

                        }
                        txtUserName.Text = string.Empty;
                        txtPassword.Text = string.Empty;
                    }
                    else
                    {
                        CommonClass.PrintError(litLoginMsg, "LOGIN_ERROR");

                    }
                }
                else
                {
                    CommonClass.PrintError(litLoginMsg, "LOGIN_ERROR");

                }

        }

        catch (Exception ex)
        {

            MicroLibrary.PrintException(litLoginMsg, ex);
        }
    }
}